<cfcomponent displayname="cfcDAO" hint="table ID column = CategoryID">

	<cfset variables.private = structNew() />

	<cffunction name="init" access="public" output="false" returntype="CategoryDAO">
		<cfargument name="KalendarConfig" type="any" required="true" hint="This is a reference to the main Kalendar Config component"/>
		<cfset variables.private.Config = arguments.KalendarConfig />

		<cfreturn this>
	</cffunction>

	<cffunction name="getByAttributesQuery" access="public" output="false" returntype="query">
		<cfargument name="CategoryID" type="numeric" required="false" />
		<cfargument name="CategoryName" type="string" required="false" />
		<cfargument name="CategoryBGColor" type="string" required="false" />
		<cfargument name="CategoryTextColor" type="string" required="false" />
		<cfargument name="orderby" type="string" required="false" />
		
		<cfset var qList = "" />		
		<cfquery name="qList" datasource="#getConfig().getDSN()#">
			SELECT
				CategoryID,
				CategoryName,
				CategoryBGColor,
				CategoryTextColor
			FROM	tblKalendarCategories
			WHERE	0=0
		
		<cfif structKeyExists(arguments,"CategoryID") and len(arguments.CategoryID)>
			AND	CategoryID = <cfqueryparam value="#arguments.CategoryID#" CFSQLType="cf_sql_varchar" />
		</cfif>
		<cfif structKeyExists(arguments,"CategoryName") and len(arguments.CategoryName)>
			AND	CategoryName = <cfqueryparam value="#arguments.CategoryName#" CFSQLType="cf_sql_varchar" />
		</cfif>
		<cfif structKeyExists(arguments,"CategoryBGColor") and len(arguments.CategoryBGColor)>
			AND	CategoryBGColor = <cfqueryparam value="#arguments.CategoryBGColor#" CFSQLType="cf_sql_varchar" />
		</cfif>
		<cfif structKeyExists(arguments,"CategoryTextColor") and len(arguments.CategoryTextColor)>
			AND	CategoryTextColor = <cfqueryparam value="#arguments.CategoryTextColor#" CFSQLType="cf_sql_varchar" />
		</cfif>
		<cfif structKeyExists(arguments, "orderby") and len(arguments.orderBy)>
			ORDER BY #arguments.orderby#
		</cfif>
		</cfquery>
		
		<cfreturn qList />
	</cffunction>

	
	<cffunction name="create" access="public" output="false" returntype="boolean">
		<cfargument name="cfc" type="any" required="true" />
		<cfset var qCreate = "" />
		<cfset arguments.cfc.setCategoryID(CreateUUID())/>
		<cftry>
			<cfquery name="qCreate" datasource="#getConfig().getDSN()#">
				INSERT INTO tblKalendarCategories
					(
					CategoryID,
					CategoryName,
					CategoryBGColor,
					CategoryTextColor
					)
				VALUES
					(
					<cfqueryparam value="#arguments.cfc.getCategoryID()#" CFSQLType="cf_sql_varchar" />,
					<cfqueryparam value="#arguments.cfc.getCategoryName()#" CFSQLType="cf_sql_varchar" />,
					<cfqueryparam value="#arguments.cfc.getCategoryBGColor()#" CFSQLType="cf_sql_varchar" null="#not len(arguments.cfc.getCategoryBGColor())#" />,
					<cfqueryparam value="#arguments.cfc.getCategoryTextColor()#" CFSQLType="cf_sql_varchar" null="#not len(arguments.cfc.getCategoryTextColor())#" />
					)
			</cfquery>
			<cfcatch type="database">
				<cfreturn false />
			</cfcatch>
		</cftry>
		<cfreturn true />
	</cffunction>

	<cffunction name="read" access="public" output="false" returntype="void">
		<cfargument name="cfc" type="any" required="true" />

		<cfset var qRead = "" />
		<cfset var strReturn = structNew() />
			<cfquery name="qRead" datasource="#getConfig().getDSN()#">
				SELECT
					CategoryID,
					CategoryName,
					CategoryBGColor,
					CategoryTextColor
				FROM	tblKalendarCategories
				WHERE	CategoryID = <cfqueryparam value="#arguments.cfc.getCategoryID()#" CFSQLType="cf_sql_varchar" />
			</cfquery>
			
		<cfif qRead.recordCount>
			<cfset strReturn = queryRowToStruct(qRead)>
			<cfset arguments.cfc.init(argumentCollection=strReturn)>
		</cfif>
	</cffunction>

	<cffunction name="update" access="public" output="false" returntype="boolean">
		<cfargument name="cfc" type="any" required="true" />

		<cfset var qUpdate = "" />
		<cftry>
			<cfquery name="qUpdate" datasource="#getConfig().getDSN()#">
				UPDATE	tblKalendarCategories
				SET
					CategoryName = <cfqueryparam value="#arguments.cfc.getCategoryName()#" CFSQLType="cf_sql_varchar" />,
					CategoryBGColor = <cfqueryparam value="#arguments.cfc.getCategoryBGColor()#" CFSQLType="cf_sql_varchar" null="#not len(arguments.cfc.getCategoryBGColor())#" />,
					CategoryTextColor = <cfqueryparam value="#arguments.cfc.getCategoryTextColor()#" CFSQLType="cf_sql_varchar" null="#not len(arguments.cfc.getCategoryTextColor())#" />
				WHERE	CategoryID = <cfqueryparam value="#arguments.cfc.getCategoryID()#" CFSQLType="cf_sql_varchar" />
			</cfquery>
			<cfcatch type="database">
				<cfreturn false />
			</cfcatch>
		</cftry>
		<cfreturn true />
	</cffunction>

	<cffunction name="delete" access="public" output="false" returntype="boolean">
		<cfargument name="cfc" type="any" required="true" />

		<cfset var qDelete = "">
		<cftry>
			<cfquery name="qDelete" datasource="#getConfig().getDSN()#">
				DELETE FROM	tblKalendarCategories 
				WHERE	CategoryID = <cfqueryparam value="#arguments.cfc.getCategoryID()#" CFSQLType="cf_sql_varchar" />
			</cfquery>
			<cfcatch type="database">
				<cfreturn false />
			</cfcatch>
		</cftry>
		<cfreturn true />
	</cffunction>

	<cffunction name="exists" access="public" output="false" returntype="boolean">
		<cfargument name="cfc" type="any" required="true" />

		<cfset var qExists = "">
		<cfquery name="qExists" datasource="#getConfig().getDSN()#" maxrows="1">
			SELECT count(1) as idexists
			FROM	tblKalendarCategories
			WHERE	CategoryID = <cfqueryparam value="#arguments.cfc.getCategoryID()#" CFSQLType="cf_sql_varchar" />
		</cfquery>

		<cfif qExists.idexists>
			<cfreturn true />
		<cfelse>
			<cfreturn false />
		</cfif>
	</cffunction>

	<cffunction name="save" access="public" output="false" returntype="boolean">
		<cfargument name="cfc" type="any" required="true" />
		
		<cfset var success = false />
		<cfif exists(arguments.cfc)>
			<cfset success = update(arguments.cfc) />
		<cfelse>
			<cfset success = create(arguments.cfc) />
		</cfif>
		
		<cfreturn success />
	</cffunction>

	<cffunction name="queryRowToStruct" access="private" output="false" returntype="struct">
		<cfargument name="qry" type="query" required="true">
		
		<cfscript>
			/**
			 * Makes a row of a query into a structure.
			 * 
			 * @param query 	 The query to work with. 
			 * @param row 	 Row number to check. Defaults to row 1. 
			 * @return Returns a structure. 
			 * @author Nathan Dintenfass (nathan@changemedia.com) 
			 * @version 1, December 11, 2001 
			 */
			//by default, do this to the first row of the query
			var row = 1;
			//a var for looping
			var ii = 1;
			//the cols to loop over
			var cols = listToArray(qry.columnList);
			//the struct to return
			var stReturn = structnew();
			//if there is a second argument, use that for the row number
			if(arrayLen(arguments) GT 1)
				row = arguments[2];
			//loop over the cols and build the struct from the query row
			for(ii = 1; ii lte arraylen(cols); ii = ii + 1){
				stReturn[cols[ii]] = qry[cols[ii]][row];
			}		
			//return the struct
			return stReturn;
		</cfscript>
	</cffunction>

	<cffunction name="getConfig" access="private" output="false" returntype="any">
		<cfreturn variables.private.Config />
	</cffunction>
		
</cfcomponent>